package internationalenergytradingplatform.controller.BuyerController;

import internationalenergytradingplatform.VO.BuyerHomeCommodityVO;
import internationalenergytradingplatform.context.BaseContext;
import internationalenergytradingplatform.dao.BuyerAccountDao;
import internationalenergytradingplatform.dao.BuyerMapper;
import internationalenergytradingplatform.dao.CommodityDao;
import internationalenergytradingplatform.dao.SupplierAccountDao;
import internationalenergytradingplatform.pojo.BuyerAccount;
import internationalenergytradingplatform.pojo.Commodity;
import internationalenergytradingplatform.pojo.SupplierAccount;
import internationalenergytradingplatform.result.Result;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/buyer-home")
@CrossOrigin
@Slf4j
public class BuyerHomeController {

    @Autowired
    BuyerMapper buyerMapper;

     @Autowired
     CommodityDao commodityDao;

     @Autowired
     SupplierAccountDao supplierAccountDao;
    /**
     *
     * @author yingfeng
     * @date 2024/3/22 17:14
     向home页面传递部分商品
     */
    @GetMapping("/commodity")
    public Result<List<BuyerHomeCommodityVO>> getPartOfCommodits(Integer number){
         //获取所有商品列表
         List<Commodity> allCommodity = commodityDao.getAllCommodity();
         //从商品列表中截取number个商品
         List<Commodity> partCommodity = allCommodity.subList(0, number);
         //创建List<BuyerHomeCommodityVO>
         List<BuyerHomeCommodityVO> commodityVOList = new ArrayList<>();
         for(Commodity commodity: partCommodity){
              BuyerHomeCommodityVO buyerHomeCommodityVO = new BuyerHomeCommodityVO();
              buyerHomeCommodityVO.setId(commodity.getId());
              buyerHomeCommodityVO.setName(commodity.getName());
              buyerHomeCommodityVO.setDescribe(commodity.getDescribe());
              buyerHomeCommodityVO.setPrice(commodity.getPrice());
              buyerHomeCommodityVO.setUnitPrice(commodity.getUnitPrice());
              buyerHomeCommodityVO.setSupplierAccount(commodity.getSupplierAccount());
              buyerHomeCommodityVO.setImage1(commodity.getImage1());
              //获取供应商信息
             SupplierAccount companyByTele = supplierAccountDao.getCompanyByTele(commodity.getSupplierAccount());
             buyerHomeCommodityVO.setSupplierName(companyByTele.getCompanyName());
             commodityVOList.add(buyerHomeCommodityVO);
         }
         log.info("响应给客户端的商品数据：{}", commodityVOList);
         return Result.success(commodityVOList);
    }

    /**
     * 查询所有供应商
     */
    @GetMapping("/supplier")
    public Result<List<SupplierAccount>> getAllSupplier(){
        List<SupplierAccount> allSupplier = supplierAccountDao.getAllSupplier();
        log.info("查询所有供应商");
        return Result.success(allSupplier);
    }

    /**
     * 根据电话查询用户信息
     */
    @GetMapping("/information")
    public Result<BuyerAccount> getBuyerAccountByTele(){
        String tele = BaseContext.getCurrentId();//获取手机号
        log.info("根据电话号码查询采购商信息:{}",tele);
        BuyerAccount buyerAccount=buyerMapper.getCompanyByTele(tele);
        return Result.success(buyerAccount);
    }

}
